package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.ImgUtils;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.SelectBankCardActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.SelectBankCardModel;
import com.diudiutechnology.www.yadiudiu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/22.
 */

public class SelectBankCardActivityAdapter extends BaseAdapter {

    private Context mContext;

    List<SelectBankCardModel> model= new ArrayList<>();
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    public SelectBankCardActivityAdapter(Context context) {
        this.mContext = context;
        options = ImgUtils.imageOptions();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    public void addItem( List<SelectBankCardModel> model){

        this.model=model;
        notifyDataSetChanged();
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.itme_select_bank_card, null);
            holder.imte_myaddress_text = (TextView) convertView
                    .findViewById(R.id.imte_myaddress_text);
            holder.imte_number = (TextView) convertView
                    .findViewById(R.id.imte_number);
            holder.imte_myaddress = (ImageView) convertView
                    .findViewById(R.id.imte_myaddress);
            holder.lien = (TextView) convertView
                    .findViewById(R.id.lien);
            holder.item_img = (ImageView) convertView
                    .findViewById(R.id.item_img);
            holder.delete = (TextView) convertView
                    .findViewById(R.id.delete);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position==(model.size()-1)){
                holder.lien.setVisibility(View.INVISIBLE);
        }else {
            holder.lien.setVisibility(View.VISIBLE);
        }
        final SelectBankCardModel select = model.get(position);
        holder.imte_myaddress_text.setText(select.getBankname());
        if (!Sundry.getStinges(select.getBanknumber())){
            String banknumber = select.getBanknumber();
            String  str=banknumber.substring(banknumber.length()-4);
            holder.imte_number.setText("尾号 "+str+" 储蓄卡");
        }

        if (select.getIs_default().equals("2")){
            holder.imte_myaddress.setVisibility(View.VISIBLE);
        }else {
            holder.imte_myaddress.setVisibility(View.GONE);
        }
        imageLoader.displayImage(Constant.HTTP+select.getBanklogo(), holder.item_img, options);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((SelectBankCardActivity)mContext).processdeletingr(select.getId());
            }
        });
        return convertView;
    }

    class ViewHolder {


        ImageView imte_myaddress;
        ImageView item_img;
        TextView imte_number;
        TextView imte_myaddress_text;
        TextView lien;
        TextView delete;
    }
}


